<script lang="ts" setup>
import { useVbenModal } from '@vben/common-ui';

import { message, TreeSelect } from 'ant-design-vue';
import { $t } from '#/locales';
import { useVbenForm } from '#/adapter/form';
import { postNavMenus, putNavMenu } from '#/api/admin/navMenus';

const menus = ref();
const navs = ref();
const [Form, formApi] = useVbenForm({
  // 所有表单项共用，可单独在表单内覆盖
  commonConfig: {
    labelClass: 'w-2/6',
  },
  handleSubmit: onSubmit,
  schema: [
    {
      component: 'Input',
      dependencies: {
        show: false,
        // 随意一个字段改变时，都会触发
        triggerFields: ['id'],
      },
      fieldName: 'id',
      label: 'ID',
    },
    {
      component: 'Input',
      dependencies: {
        show: false,
        // 随意一个字段改变时，都会触发
        triggerFields: ['nav_id'],
      },
      fieldName: 'nav_id',
      label: 'nav_id',
    },
    {
      component: 'TreeSelect',
      fieldName: 'parent_id',
      label: $t('admin.Parent'),
      rules: 'required',
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: $t('page.common.Please Enter'),
      },
      fieldName: 'name',
      label: $t('page.common.Name'),
      rules: 'required',
    },
    {
      component: 'InputIcon',
      fieldName: 'icon',
      label: $t('page.common.Icon'),
    },
    {
      component: 'TreeSelect',
      dependencies: {
        disabled(values) {
          return !!values.external_href;
        },
        triggerFields: ['external_href'],
      },
      fieldName: 'href',
      label: $t('admin.Category'),
    },
    {
      component: 'Input',
      dependencies: {
        disabled(values) {
          return !!values.href;
        },
        triggerFields: ['href'],
      },
      componentProps: {
        placeholder: $t('page.common.Please Enter'),
      },
      fieldName: 'external_href',
      label: $t('admin.Link'),
    },

    {
      component: 'Select',
      defaultValue: '',
      componentProps: {
        class: 'w-full',
        placeholder: $t('page.common.Please Enter'),
        options: [
          {
            label: '新窗口(_blank)',
            value: '_blank',
          },
          {
            label: $t('admin.Default'),
            value: '',
          },
        ],
      },
      fieldName: 'target',
      label: $t('admin.Open Method'),
    },
    {
      component: 'Select',
      defaultValue: 0,
      componentProps: {
        class: 'w-full',
        placeholder: $t('page.common.Please Enter'),
        options: [
          {
            label: $t('page.common.No'),
            value: 0,
          },
          {
            label: $t('page.common.Yes'),
            value: 1,
          },
        ],
        
        
      },
      fieldName: 'is_social',
      label: $t('admin.Is Social Media'),
    },
    {
      component: 'Upload',
      defaultValue: [],
      componentProps: {
        placeholder: $t('page.common.Please Enter'),

      },
        
         dependencies: {
        if(values) {
          return !!values.is_social;
        },
        triggerFields: ['is_social'],
      },
    
      fieldName: 'thumbnails',
      label: $t('admin.Social Media Image'),
    },
  ],
  showDefaultActions: false,
  // 中屏一行显示2个，小屏一行显示1个
  wrapperClass: 'grid-cols-1 md:grid-cols-2',
});

const [Modal, formModalApi] = useVbenModal({
  fullscreenButton: false,
  onCancel() {
    formModalApi.close();
  },
  onConfirm: async () => {
    await formApi.validateAndSubmitForm();
    formModalApi.close();
  },
  onOpenChange(isOpen: boolean) {
    if (isOpen) {
      const values: any = formModalApi.getData();
      if (values) {
        menus.value = values.menus;
        navs.value = values.navs;

        formApi.setValues(values.form);
      }
    }
  },
});
async function onSubmit(values: any) {
  function showMsg(msg: string) {
    message.success(msg);
    formModalApi.setData({ isUpdata: true });
  }

  if (values.id) {
    const { msg } = await putNavMenu(values.id, values);
    showMsg(msg);
  } else {
    const { msg } = await postNavMenus(values);
    showMsg(msg);
  }
}
async function onNavsChange(value: any, node: any) {
  formApi.setFieldValue('href', value);
  const formValue = await formApi.getValues();
  if (!formValue?.name) {
    formApi.setFieldValue('name', node.name);
  }
}
</script>
<template>
  <Modal class="w-3/5">
    <Form>
      <template #parent_id="slotProps">
        <TreeSelect
          v-bind="slotProps"
          :field-names="{
            value: 'id',
            key: 'id',
            label: 'name',
            children: 'children',
          }"
          :tree-data="menus"
          allow-clear
          placeholder="选择导航"
          show-search
          style="width: 100%"
          tree-default-expand-all
          tree-node-filter-prop="label"
        />
      </template>
      <template #href="slotProps">
        <TreeSelect
          v-bind="slotProps"
          :field-names="{
            value: 'rule',
            key: 'rule',
            label: 'name',
            children: 'items',
          }"
          :tree-data="navs"
          allow-clear
          placeholder="选择栏目"
          show-search
          style="width: 100%"
          tree-default-expand-all
          tree-node-filter-prop="label"
          @select="onNavsChange"
        />
      </template>
    </Form>
  </Modal>
</template>

<style lang="scss" scoped></style>
